package com.junxonline.plat.controller.device;

import com.junxonline.common.dao.vo.ResultVO;
import com.junxonline.common.util.CommonUtils;
import com.junxonline.plat.controller.base.BaseController;
import com.junxonline.plat.dao.vo.*;
import com.junxonline.plat.exception.ErrorCodeEnum;
import com.junxonline.plat.service.device.DeviceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author JunX
 * @ClassName: DeviceController
 * @Description: 设备控制器
 */

@RestController
@RequestMapping(value = "/admin/device")
public class DeviceController extends BaseController {

    @Autowired
    private DeviceService deviceService;
    
    /**
     * 获取设备数据
     * @param vo
     * @return
     */
    @RequestMapping(value = "/getDevice")
    public ResultVO getDevice(@RequestBody QueryDeviceVO vo) {

        if (CommonUtils.isEmpty(vo.getDeviceId())) {
            return getReturnError(ErrorCodeEnum.PARAMS_IS_NULL);
        }

        return getReturnSuccess(deviceService.getDevice(vo));
    }

    /**
     * 筛选查询设备数据
     * @param page
     * @param vo
     * @return
     */
    @RequestMapping(value = "/queryDevice")
    public PageResultVO queryDevice(PageVO page, QueryDeviceVO vo) {
        return getReturnSuccess(deviceService.queryDevice(page, vo));
    }


    /**
     * 添加设备
     * @param vo
     * @return
     */
    @RequestMapping(value = "/addDevice")
    public ResultVO addDevice(@RequestBody DeviceVO vo) {

        if (CommonUtils.isEmpty(vo) || CommonUtils.isEmpty(vo.getDeviceNo()) || CommonUtils.isEmpty(vo.getDeviceName()) ||
            CommonUtils.isEmpty(vo.getDeviceManufacturer()) || CommonUtils.isEmpty(vo.getDeviceStatus())) {
            return getReturnError(ErrorCodeEnum.PARAMS_IS_NULL);
        }

        Long userId = getUserId();

        boolean success = deviceService.addDevice(vo, userId);
        if (!success) {
            return getReturnError(ErrorCodeEnum.SYSTEM_ERROR);
        }

        return getReturnSuccess();

    }

    /**
     * 修改设备
     * @param vo
     * @return
     */
    @RequestMapping(value = "/editDevice")
    public ResultVO editDevice(@RequestBody DeviceVO vo) {
        if (CommonUtils.isEmpty(vo) || CommonUtils.isEmpty(vo.getDeviceId()) || CommonUtils.isEmpty(vo.getDeviceName()) ||
                CommonUtils.isEmpty(vo.getDeviceNo()) || CommonUtils.isEmpty(vo.getDeviceStatus())) {
            return getReturnError(ErrorCodeEnum.PARAMS_IS_NULL);
        }

        boolean success = deviceService.editDevice(vo);
        if (!success) {
            return getReturnError(ErrorCodeEnum.SYSTEM_ERROR);
        }

        return getReturnSuccess();

    }


    /**
     * 删除设备
     * @param vo
     * @return
     */
    @RequestMapping(value = "/delDevice")
    public ResultVO delDevice(@RequestBody DeviceIdsVO vo) {
        if (CommonUtils.isEmpty(vo)) {
            return getReturnError(ErrorCodeEnum.PARAMS_IS_NULL);
        }

        Long[] deviceIdsArray = vo.getDeviceIds();

        boolean successMenu = deviceService.delDevice(deviceIdsArray);
        if (!successMenu) {
            return getReturnError(ErrorCodeEnum.SYSTEM_ERROR);
        }

        return getReturnSuccess();
    }

    /**
     * 检修设备
     * @param vo
     * @return
     */
    @RequestMapping(value = "/addCheckDevice")
    public ResultVO addCheckDevice(@RequestBody DeviceVO vo) {
        if (CommonUtils.isEmpty(vo)) {
            return getReturnError(ErrorCodeEnum.PARAMS_IS_NULL);
        }

        Long userId = getUserId();

        boolean successMenu = deviceService.addCheckDevice(vo, userId);
        if (!successMenu) {
            return getReturnError(ErrorCodeEnum.SYSTEM_ERROR);
        }

        return getReturnSuccess();
    }

}
